package org.example.wx.service;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.example.common.utils.DateTimeUtil;
import org.example.db.entity.Collect;
import org.example.db.entity.Goods;
import org.example.db.mapper.CollectMapper;
import org.example.db.mapper.GoodsMapper;
import org.example.security.service.AuthService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WxCollectService {

    AuthService authService;
    CollectMapper collectMapper;
    GoodsMapper goodsMapper;

    public WxCollectService(AuthService authService, CollectMapper collectMapper, GoodsMapper goodsMapper) {
        this.authService = authService;
        this.collectMapper = collectMapper;
        this.goodsMapper = goodsMapper;
    }

    public int isCollect(Long goodsId){


        Long wxUserId = authService.getWxUserId();
        List<Collect> collects = collectMapper.selectList(Wrappers.lambdaQuery(Collect.class)
                .eq(Collect::getUserId, wxUserId).eq(Collect::getGoodsId, goodsId));
        return collects.size();
    }

    public List<Goods> list(){

        Long wxUserId = authService.getWxUserId();
        List<Collect> collects = collectMapper.selectList(Wrappers.lambdaQuery(Collect.class).eq(Collect::getUserId, wxUserId));
        ArrayList<Goods> goodsList = new ArrayList<>();
        for(Collect collect: collects){

            goodsList.add(goodsMapper.selectById(collect.getGoodsId()));
        }

        return goodsList;
    }

    public void add(Long goodsId){

        Long wxUserId = authService.getWxUserId();
        collectMapper.insert(new Collect(null, wxUserId, goodsId, DateTimeUtil.getDateTimeString(), DateTimeUtil.getDateTimeString()));
    }


    public void delete(Long goodsId){

        Long wxUserId = authService.getWxUserId();

        collectMapper.delete(Wrappers.lambdaQuery(Collect.class).eq(Collect::getGoodsId, goodsId).eq(Collect::getUserId, wxUserId));
    }
}
